package com.pp.proposalPrint.client;

import java.util.logging.Level;

import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SafeHtmlHeader;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.NumberLabel;
import com.pp.proposalPrint.shared.Material;
import com.pp.proposalPrint.shared.Materials;

public class MaterialTable extends ProposalTable<Materials, Material> {

	public MaterialTable(String header) {
		super(proposalContentsList, header);
	}
	
	// Sum label at the end of the table
	private final NumberLabel<Float> nLblSum = new NumberLabel<Float>();

	// Procurement expenses label at the end of the table
	private final NumberLabel<Float> nLblProcExp = new NumberLabel<Float>();
	
	// Total sum label at the end of the table
	private final NumberLabel<Float> nLblTotalMatSum = new NumberLabel<Float>();

	// List of materials
	private static Materials proposalContentsList = new Materials();
	
	// Column widths
	private static final int PROPOSAL_CONTENT_UNIT     =  40;
	private static final int PROPOSAL_CONTENT_PRICE    =  40;
	
	
	/**
	 * This is the entry point method.
	 */
	@Override
	protected void loadTable() {
		super.loadTable();
		
		// Create the unit column
		final TextInputCellStyled tIClUnit = new TextInputCellStyled(PROPOSAL_CONTENT_UNIT);
		Column<Material, String> cUnit = new Column<Material, String>(tIClUnit) {
			@Override
			public String getValue(Material object) {
				// Return the material as the value of this column.
				return Float.toString(object.getUnit());
			}
		};
		cUnit.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		ctProposalContentTable.addColumn(cUnit, "Units");
		// Add a field updater to save the new value.
		cUnit.setFieldUpdater(new FieldUpdater<Material, String>() {
			@Override
			public void update(int index, Material object, String value) {
				// Push the changes into the Material.
				object.setUnit(Float.parseFloat(value));
				// Redraw the table with the new data.
				ctProposalContentTable.redraw();
				updateProposalContentListCalculations();
				// Log the array for debugging purposes
				logger.log(Level.ALL, "[ProposalPrint]: material unit updated: id: " + object.getId());
				for(Material mtrl :  proposalContentsList.getContentList()) {
					logger.log(Level.ALL, "[ProposalPrint]: id: " + mtrl.getId() + ", unit: " + mtrl.getUnit());
				}
			}
		});
		
		// Create the unitPrice column
		final TextInputCellStyled tIClUnitPrice = new TextInputCellStyled(PROPOSAL_CONTENT_PRICE);
		Column<Material, String> cUnitPrice = new Column<Material, String>(tIClUnitPrice) {
			@Override
			public String getValue(Material object) {
				// Return the material as the value of this column.
				return Float.toString(object.getUnitPrice());
			}
		};
		cUnitPrice.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		ctProposalContentTable.addColumn(cUnitPrice, "Unit Price");
		// Add a field updater to save the new value.
		cUnitPrice.setFieldUpdater(new FieldUpdater<Material, String>() {
			@Override
			public void update(int index, Material object, String value) {
				// Push the changes into the Material.
				object.setUnitPrice(Float.parseFloat(value));
				// Redraw the table with the new data.
				ctProposalContentTable.redraw();
				updateProposalContentListCalculations();
				// Log the array for debugging purposes
				logger.log(Level.ALL, "[ProposalPrint]: material unit price updated: id: " + object.getId());
				for(Material mtrl : proposalContentsList.getContentList()) {
					logger.log(Level.ALL, "[ProposalPrint]: id: " + mtrl.getId() + ", unit price: " + mtrl.getUnitPrice());
				}
			}
		});
		
		// Create the total price column.
		final NumberCell nClTotalPrice = new NumberCell();
		Column<Material, Number> cTotalPrice = new Column<Material, Number>(nClTotalPrice) {
			@Override
			public Number getValue(Material object) {
				return object.getTotal();
			}
		};
		cTotalPrice.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		ctProposalContentTable.addColumn(cTotalPrice, new SafeHtmlHeader(SafeHtmlUtils.fromString("Total")));
	}
	
	protected void loadFooter() {
		
		// Sum of materials
		HorizontalPanel hPnlSum = new HorizontalPanel();
		hPnlSum.setBorderWidth(1);
		vPnlProposalContentList.add(hPnlSum);
		vPnlProposalContentList.setCellHorizontalAlignment(hPnlSum, HasHorizontalAlignment.ALIGN_RIGHT);
		
		Label lblSum = new Label("Sum:");
		hPnlSum.add(lblSum);
		nLblSum.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		hPnlSum.add(nLblSum);
		hPnlSum.setCellHorizontalAlignment(nLblSum, HasHorizontalAlignment.ALIGN_RIGHT);
		nLblSum.setWordWrap(false);
		vPnlProposalContentList.setCellHorizontalAlignment(nLblSum, HasHorizontalAlignment.ALIGN_RIGHT);
		nLblSum.setValue(proposalContentsList.getSum());

		// Procurement expenses	
		HorizontalPanel hPnlProcExp = new HorizontalPanel();
		hPnlProcExp.setBorderWidth(1);
		vPnlProposalContentList.add(hPnlProcExp);
		vPnlProposalContentList.setCellHorizontalAlignment(hPnlProcExp, HasHorizontalAlignment.ALIGN_RIGHT);
		
		Label lblProcExp = new Label("Procurement Expenses 10%:");
		hPnlProcExp.add(lblProcExp);
		nLblProcExp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		hPnlProcExp.add(nLblProcExp);
		hPnlProcExp.setCellHorizontalAlignment(nLblProcExp, HasHorizontalAlignment.ALIGN_RIGHT);
		nLblProcExp .setWordWrap(false);
		vPnlProposalContentList.setCellHorizontalAlignment(nLblProcExp, HasHorizontalAlignment.ALIGN_RIGHT);
		nLblProcExp.setValue(proposalContentsList.getProcExp());
		
		// Total sum of materials.
		HorizontalPanel hPnlTotalMatSum = new HorizontalPanel();
		hPnlTotalMatSum.setBorderWidth(1);
		vPnlProposalContentList.add(hPnlTotalMatSum);
		vPnlProposalContentList.setCellHorizontalAlignment(hPnlTotalMatSum, HasHorizontalAlignment.ALIGN_RIGHT);
		
		Label lblTotal = new Label("Total:");
		hPnlTotalMatSum.add(lblTotal);
		nLblTotalMatSum.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		hPnlTotalMatSum.add(nLblTotalMatSum);
		hPnlTotalMatSum.setCellHorizontalAlignment(nLblTotalMatSum, HasHorizontalAlignment.ALIGN_RIGHT);
		nLblTotalMatSum.setWordWrap(false);
		vPnlProposalContentList.setCellHorizontalAlignment(nLblTotalMatSum, HasHorizontalAlignment.ALIGN_RIGHT);
		nLblTotalMatSum.setValue(proposalContentsList.getTotalSum());
	}
	
	@Override
	protected void updateProposalContentListCalculations() {
		nLblSum.setValue(proposalContentsList.getSum());
		nLblProcExp.setValue(proposalContentsList.getProcExp());
		nLblTotalMatSum.setValue(proposalContentsList.getTotalSum());
	}

	@Override
	public void build() {
		loadTable();
		loadLastColumn();
		loadFooter();
	}

}
